package com.mall.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.mall.common.ServerResponse;
import com.mall.dao.MmallCategoryMapper;
import com.mall.pojo.MmallCategory;
import com.mall.service.ICategoryService;

@Service
public class CategoryServiceImpl implements ICategoryService {
	
	@Autowired
	private MmallCategoryMapper mapper;
	
	Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);
	
	@Override
	public ServerResponse<String> addCategory(String categoryName,Integer parentId) {
		
		if(parentId == null || StringUtils.isBlank(categoryName)){
			return ServerResponse.createByErrorMessage("添加品类参数错误");
		}
		
		MmallCategory category = new MmallCategory(); 
		category.setParentId(parentId);
		category.setName(categoryName);
		//类别状态: 1-正常,2-已废弃'
		category.setStatus(true);
		
		int count = mapper.insert(category);
		if(count == 0 ){
			return ServerResponse.createByErrorMessage("增加品类失败");
		}
		return ServerResponse.createBySuccess("增加品类成功");
		
	}

	@Override
	public ServerResponse<String> updateCategory(String categoryName, Integer categoryId) {
		
		if(categoryId == null || StringUtils.isBlank(categoryName) ){
			return ServerResponse.createByErrorMessage("更新品类参数错误");
		}
		
		MmallCategory category = new MmallCategory();
		category.setName(categoryName);
		category.setId(categoryId);
		
		
		int count = mapper.updateByPrimaryKeySelective(category );
		if(count == 0){
			return ServerResponse.createByErrorMessage("更新品类失败");
		}
		return ServerResponse.createBySuccess("更新品类成功");
	}

	@Override
	public ServerResponse<List<MmallCategory>> getCategory(Integer categoryId) {
		
		List<MmallCategory> list = mapper.selectCategoryChildrenByParentId(categoryId);
		
		if(CollectionUtils.isEmpty(list) ){
			logger.info("未找到当前分类的子分类");
		}
		return ServerResponse.createBySuccess(list);
	}
	

	@Override			//获取当前分类id及递归子节点categoryId
	public ServerResponse<List<Integer>> getDeepCategory(Integer categoryId) {
		
		Set<MmallCategory> set = new HashSet<>();
		findChildCategory(set, categoryId);
		
		List<Integer> list = new ArrayList<>();
		if(categoryId != null){
			for(MmallCategory ca : set){
				list.add(ca.getId());
			}
		}
		
		return ServerResponse.createBySuccess(list);
	}
	
	//递归算法,算出子节点 
	private Set<MmallCategory> findChildCategory(Set<MmallCategory> set, Integer categoryId){
		
		MmallCategory category = mapper.selectByPrimaryKey(categoryId);
		if(category != null){
			set.add(category);
		}
		
		//查找子节点,递归算法一定要有一个退出的条件
		List<MmallCategory> lists = mapper.selectCategoryChildrenByParentId(categoryId);
		for(MmallCategory ca : lists){
			findChildCategory(set, ca.getId());
		}
		return set;
	}
	
	
	
	
}
